package xhwl.insurance.ui.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import xhwl.android.common.util.ImgLoaderUtil;
import xhwl.insurance.R;
import xhwl.insurance.adapter.SaleCardDetailListAdapter;
import xhwl.insurance.adapter.SaleCardMenuListAdapter;
import xhwl.insurance.http.api.FetchSalesCardDataApi;
import xhwl.insurance.http.bean.resp.SalesCardData;
import xhwl.insurance.util.AppUtil;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;


public class SaleCardFragment extends BaseFragment {
    private ListView listview;
    private ListView pullToRefreshListView;
    private LinearLayout empty_list_view;
    List<SalesCardData> mlist = new ArrayList<>();
    List<SalesCardData.DataBeanX.DataBean> detailList = new ArrayList<>();
    List<SalesCardData.DataBeanX> tempList = new ArrayList<>();
    private ImageView img_top;
    private SaleCardDetailListAdapter adapter;
    private SaleCardMenuListAdapter menuAdapter;

    RelativeLayout rlContent;
    LinearLayout llNetUnavalible;
    TextView tvRefresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_salescard, null);
        return view;
    }

    @Override
    protected void initView(View view) {
        initTitleViews(view);
        rlContent= (RelativeLayout) view.findViewById(R.id.rlContent);
        llNetUnavalible= (LinearLayout) view.findViewById(R.id.llNetUnavalible);
        tvRefresh= (TextView) view.findViewById(R.id.tvRefresh);

        listview = (ListView) view.findViewById(R.id.listview);
        pullToRefreshListView = (ListView) view.findViewById(R.id.pullToRefreshListView);
//        empty_list_view = (TextView) view.findViewById(R.id.empty_list_view);
        empty_list_view = (LinearLayout) view.findViewById(R.id.emptyView);
        img_top = (ImageView) view.findViewById(R.id.img_top);
        pullToRefreshListView.setEmptyView(empty_list_view);
        pullToRefreshListView.setOverScrollMode(View.OVER_SCROLL_NEVER);

    }

    @Override
    protected void setUpTitleView() {
        titleTextView.setText("首页");
        llBack.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void initListener() {
    }

    @Override
    protected void processOper() {
        super.processOper();
        menuAdapter = new SaleCardMenuListAdapter(getActivity(), mlist);
        listview.setAdapter(menuAdapter);
        adapter = new SaleCardDetailListAdapter(getActivity(), detailList);
        pullToRefreshListView.setAdapter(adapter);
    }

    @Override
    protected void loadData() {
        if(!AppUtil.iskNetAvailable(mActivity)){
            rlContent.setVisibility(View.GONE);
            llNetUnavalible.setVisibility(View.VISIBLE);
            tvRefresh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    loadData();
                }
            });
            return;
        }
        else
        {
            rlContent.setVisibility(View.VISIBLE);
            llNetUnavalible.setVisibility(View.GONE);
        }
        requestData();
    }



    private void requestData() {
        FetchSalesCardDataApi api = new FetchSalesCardDataApi();
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<ArrayList<SalesCardData>>() {
            @Override
            public void onNext(ArrayList<SalesCardData> resp) {
                fetchListData(resp);
            }
        });

    }

    /*
    * 根据yw_id去请求详细列表信息
    * */
    private void getDetailData(String yw_id) {
        tempList.clear();
        detailList.clear();
        for (int i = 0; i < mlist.size(); i++) {
            if (mlist.get(i).getYw_id().equals(yw_id)) {
                tempList.addAll(mlist.get(i).getData());
            }
        }
        for (int i = 0; i < tempList.size(); i++) {
            if (tempList.get(i).getData().get(0) != null)
                tempList.get(i).getData().get(0).setGroupName(tempList.get(i).getGroup());//将每个组的第一个加入组名
            detailList.addAll(tempList.get(i).getData());
        }
        adapter.notifyDataSetChanged();
    }


    private void fetchListData(final ArrayList<SalesCardData> totalDataList) {
        mlist.clear();
        mlist.addAll(totalDataList);
        menuAdapter.notifyDataSetChanged();
        menuAdapter.setDefSelect(0);//设置默认选中第一项
        if (null != mlist.get(0).getCard_pic()) {
            ImgLoaderUtil.getInstance().loadImageAutoResize(mlist.get(0).getCard_pic(),img_top.getWidth(),img_top);
        } else {
            imgTopShow(mlist.get(0).getYw_id());
        }
        if (null != mlist.get(0).getYw_id()) {
            getDetailData(mlist.get(0).getYw_id());
        }
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                menuAdapter.setDefSelect(position);//设置默认选中第一项
                String yw_id = mlist.get(position).getYw_id();
                getDetailData(yw_id);
                if (null != mlist.get(position).getCard_pic()) {
                    Glide.with(getActivity())
                            .load(mlist.get(position).getCard_pic())
                            .error(R.drawable.card_heart)
                            .into(img_top);
                } else {
                    imgTopShow(yw_id);
                }

            }
        });
    }

    //根据不同的yw_id右侧详细界面顶部imageview显示相应的图片
    private void imgTopShow(String yw_id) {
        if (yw_id.equals("1")) {
            img_top.setImageDrawable(getResources().getDrawable(R.drawable.card_eye));
        } else if (yw_id.equals("2")) {
            img_top.setImageDrawable(getResources().getDrawable(R.drawable.card_heart));
        } else if (yw_id.equals("5")) {
            img_top.setImageDrawable(getResources().getDrawable(R.drawable.card_lung));
        } else if (yw_id.equals("6")) {
            img_top.setImageDrawable(getResources().getDrawable(R.drawable.card_liver));
        } else if (yw_id.equals("21")) {
            img_top.setImageDrawable(getResources().getDrawable(R.drawable.card_female));
        }
    }
}
